{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "import json\n",
    "from flask import Flask, render_template\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Map\n",
    "from flask.json import jsonify\n",
    "from flask import Flask, render_template, request\n",
    "from flask import Flask\n",
    "from jinja2 import Markup, Environment, FileSystemLoader\n",
    "from pyecharts.globals import CurrentConfig\n",
    "import random\n",
    "import time\n",
    "from flask_cors import CORS\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import WordCloud\n",
    "from pyecharts.globals import SymbolType\n",
    "import requests\n",
    "import json\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "def solve_scal(x):\n",
    "    sort_list = ['1-49人',\n",
    "                 '50-99人',\n",
    "                 '100-499人',\n",
    "                 '500-999人',\n",
    "                 '1000-2000人',\n",
    "                 '2000-5000人',\n",
    "                 '10000人以上',\n",
    "                 '5000-10000人'\n",
    "                 ]\n",
    "\n",
    "    empty_list = []\n",
    "    new_list = []\n",
    "    for y in x:\n",
    "        if y == sort_list[0]:\n",
    "            empty_list.append(int(0))\n",
    "        elif y == sort_list[1]:\n",
    "            empty_list.append(int(1))\n",
    "        elif y == sort_list[2]:\n",
    "            empty_list.append(int(2))\n",
    "        elif y == sort_list[3]:\n",
    "            empty_list.append(int(3))\n",
    "        elif y == sort_list[4]:\n",
    "            empty_list.append(int(4))\n",
    "        elif y == sort_list[5]:\n",
    "            empty_list.append(int(5))\n",
    "        elif y == sort_list[6]:\n",
    "            empty_list.append(int(6))\n",
    "        elif y == sort_list[7]:\n",
    "            empty_list.append(int(7))\n",
    "    get_list = sorted(empty_list)\n",
    "    for i in range(len(get_list)):\n",
    "        new_list.append(sort_list[get_list[i]])\n",
    "    return new_list"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def create_chart(job_name, city_name):\n",
    "    import pandas\n",
    "    from requests_html import HTMLSession\n",
    "    import json\n",
    "    import pandas as pd\n",
    "    import requests\n",
    "    import time\n",
    "    time.localtime()\n",
    "    output_time = str(time.localtime().tm_year)+'-'\\\n",
    "                  +str(time.localtime().tm_mon)+'-'\\\n",
    "                 +str(time.localtime().tm_mday)\n",
    "    output_time\n",
    "    df = pd.read_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-{output_time}.xlsx')\n",
    "    # 城市各学历岗位个数 = df.groupby(['地区','学历']).agg({'职位':'count'})\n",
    "    # 城市各学历岗位个数.to_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-各地区学历对应岗位个数-{output_time}.xlsx')\n",
    "    import numpy as np\n",
    "    整个城市学历分布 = df.groupby(['学历']).agg({'职位':'count'})\n",
    "    整个城市学历分布.to_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市学历对应岗位个数-{output_time}.xlsx')\n",
    "    df_xueli = pd.read_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市学历对应岗位个数-{output_time}.xlsx')\n",
    "    df_xueli.assign(**{city_name: ''})\n",
    "    df_xueli = df_xueli.reindex(columns=[city_name, '学历', '职位'], fill_value='')\n",
    "    df_xueli.to_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市学历对应岗位个数-{output_time}.xlsx')\n",
    "    城市各工作经验岗位个数 = df.groupby(['工作年限']).agg({'职位':'count'})\n",
    "    城市各工作经验岗位个数.to_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市工作经验对应岗位个数-{output_time}.xlsx')\n",
    "    df_exper = pd.read_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市工作经验对应岗位个数-{output_time}.xlsx')\n",
    "    df_exper.assign(**{city_name: ''})\n",
    "    df_exper = df_exper.reindex(columns=[city_name, '工作年限', '职位'], fill_value='')\n",
    "    df_exper.to_excel(f'excels/{city_name}/{job_name}/{output_time}/猎聘{city_name}-{job_name}-城市工作经验对应岗位个数-{output_time}.xlsx')\n",
    "    from requests_html2 import HTMLSession\n",
    "    session = HTMLSession()\n",
    "\n",
    "    # import time\n",
    "    # import random\n",
    "    # sleep_time = random.randint(3,5)\n",
    "    df['地区'].value_counts()\n",
    "    地区 = [df['地区'].value_counts().index.tolist()[i].split('-')[1] \\\n",
    "            for i, v in enumerate(df['地区'].value_counts().index.tolist()) if '-' in v]\n",
    "    地区\n",
    "    岗位个数 = [df['地区'].value_counts().values.tolist()[i] for i, v in\n",
    "                enumerate(df['地区'].value_counts().index.tolist()) if \"-\" in v]\n",
    "    岗位个数\n",
    "    df['职位'][df['职位'].str.contains('（')].str.split('（').apply(lambda x: x[0])\n",
    "    df_job_title = df['职位'].apply(lambda x: x.split('（')[0].split('/')[0].split('(')[0]).value_counts()\n",
    "    df_job_title.index.tolist()\n",
    "    len(df_job_title.index.tolist())\n",
    "    df_job_title.values.tolist()\n",
    "    df['职位'].value_counts()\n",
    "    PM_title_words = [(df_job_title.index.tolist()[i], df_job_title.values.tolist()[i]) for i in\n",
    "                      range(1, len(df_job_title.index.tolist()))]\n",
    "    PM_title_words\n",
    "    df['职位标签']\n",
    "    df['职位标签'].values\n",
    "    df['职位标签'].apply(lambda x: eval(x)).tolist()\n",
    "    PM_labels_list = [j for i in df['职位标签'].apply(lambda x: eval(x)).tolist() for j in i]\n",
    "    PM_labels_list\n",
    "    PM_labels_words = [(i, PM_labels_list.count(i)) for i in set(PM_labels_list)]\n",
    "    PM_labels_words\n",
    "    df_cities = df[['地区', '规模', '行业', '公司名称', '职位', '薪资']]\n",
    "    df_cities\n",
    "    df_cities = df_cities.rename(columns={\n",
    "        '地区': '地区',\n",
    "        '规模': '公司规模',\n",
    "        '行业': '公司行业',\n",
    "        '公司名称': '公司名称',\n",
    "        '职位': '岗位名称',\n",
    "        '薪资': '工资',\n",
    "\n",
    "    })\n",
    "    set_dq = set(df_cities['地区'])\n",
    "    list_dq = list(set_dq)\n",
    "    list_dq\n",
    "    data_all = [{\n",
    "        'name': city_name,\n",
    "        'children': [\n",
    "            {'name': a,\n",
    "             'children': [\n",
    "                 {\n",
    "                     'name': \"公司规模:\" + b,\n",
    "                     'children': [\n",
    "                         {\n",
    "                             'name': \"公司行业:\" + str(c),\n",
    "                             'children': [\n",
    "                                 {\n",
    "                                     'name': \"公司名称:\" + d,\n",
    "                                     'children': [\n",
    "                                         {\n",
    "                                             'name': \"岗位名称:\" + e,\n",
    "                                             'children': [\n",
    "                                                 {\n",
    "                                                     'name': \"工资：\" + f,\n",
    "                                                 } for f in list(set(df_cities[(df_cities['地区'] == a) & (\n",
    "                                                             df_cities['公司规模'] == b) & (df_cities[\n",
    "                                                                                                '公司行业'] == c) & (\n",
    "                                                                                           df_cities[\n",
    "                                                                                               '公司名称'] == d) & (\n",
    "                                                                                           df_cities['岗位名称'] == e)][\n",
    "                                                                         '工资']))\n",
    "                                             ]\n",
    "                                         } for e in list(set(df_cities[(df_cities['地区'] == a) & (\n",
    "                                                     df_cities['公司规模'] == b) & (df_cities['公司行业'] == c) & (\n",
    "                                                                                   df_cities['公司名称'] == d)][\n",
    "                                                                 '岗位名称']))\n",
    "                                     ]\n",
    "                                 } for d in list(set(df_cities[\n",
    "                                                         (df_cities['地区'] == a) & (df_cities['公司规模'] == b) & (\n",
    "                                                                     df_cities['公司行业'] == c)]['公司名称']))\n",
    "                             ]\n",
    "                         } for c in\n",
    "                         list(set(df_cities[(df_cities['地区'] == a) & (df_cities['公司规模'] == b)]['公司行业']))\n",
    "                     ]\n",
    "                 } for b in solve_scal(list(set(df_cities[(df_cities['地区'] == a)]['公司规模'])))\n",
    "             ]\n",
    "             } for a in list(set(df_cities['地区']))\n",
    "        ]\n",
    "    }\n",
    "    ]\n",
    "    data_all\n",
    "    from pyecharts import options as opts\n",
    "    from pyecharts.charts import Tree\n",
    "    # a = (\n",
    "    #     Tree()\n",
    "    #     .add(\"\", data_all, collapse_interval=1, layout=\"radial\")\n",
    "    #     .set_global_opts(title_opts=opts.TitleOpts(title=city_name + '-' + job_name + '-' + \"岗位速查树状图\"))\n",
    "    #     .render(f'charts/{city_name}_{job_name}_{output_time}_tree.html')\n",
    "    # )\n",
    "    import os\n",
    "    folder_name = f\"excels/{city_name}/{job_name}/{output_time}/charts\"\n",
    "    os.makedirs(folder_name, exist_ok=True)\n",
    "\n",
    "    a = (\n",
    "        Tree()\n",
    "        .add(\"\", data_all, collapse_interval=1, layout=\"radial\")\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=city_name + '-' + job_name + '-' + \"岗位速查树状图\"))\n",
    "        .render(f'excels/{city_name}/{job_name}/{output_time}/charts/猎聘{city_name}-{job_name}-{output_time}_tree.html')\n",
    "    )\n",
    "    b = (\n",
    "        Map()\n",
    "        .add(str(city_name), [list(z) for z in zip(地区, 岗位个数)], str(city_name))\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=city_name + '-' + job_name + '-' \"岗位分布地图\"),\n",
    "            visualmap_opts=opts.VisualMapOpts()\n",
    "        )\n",
    "        .render(f'excels/{city_name}/{job_name}/{output_time}/charts/猎聘{city_name}-{job_name}-{output_time}_map.html')\n",
    "    )\n",
    "    d = (\n",
    "        WordCloud()\n",
    "        .add(\"\", PM_title_words, word_size_range=[20, 100], shape=SymbolType.DIAMOND)\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=city_name + '-' + job_name + '-' + \"热门岗位词云图\"))\n",
    "        .render(f'excels/{city_name}/{job_name}/{output_time}/charts/猎聘{city_name}-{job_name}-{output_time}_word.html')\n",
    "    )\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广告设计-广州\n",
      "广告设计-北京\n",
      "广告设计-上海\n",
      "广告设计-深圳\n"
     ]
    }
   ],
   "source": [
    "job_list = [\"广告设计\"]\n",
    "cities_list = [\"广州\",\"北京\",\"上海\",\"深圳\"]\n",
    "import random\n",
    "wait_time = random.randint(10,15)\n",
    "for x in job_list:\n",
    "    for y in cities_list:\n",
    "        create_chart(x,y)\n",
    "        print(str(x)+'-'+str(y))\n",
    "        time.sleep(wait_time)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
